/*
 * Created on Feb 16, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import ModuleBuilder.*;
import javax.swing.event.*;


import java.beans.*;

import ModuleBuilder.*;
import ModuleBuilder.model.*;
// import ModuleBuilder.UI.TerrainTileTray.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class MapBuilder extends JPanel {
	
	private MapCanvas canvas;
	
	private JScrollPane mapScrollPane;
	private TerrainTileTray trayPanel = new TerrainTileTray();
	BuilderTrayModel trayMod;
	BuilderManager manager;
	ImageElement[][]	mapGrid;
	int mapTileWidth= ImagePreview._previewIconSize;
	int mapTileHeight= ImagePreview._previewIconSize;
	int numCol, numRow;
	Dimension mapSize;
	ImageElement currentMapTile;
	
	public MapBuilder (BuilderManager m) {
		manager = m;
		trayMod = new BuilderTrayModel();
		canvas = new MapCanvas(this);
		mapScrollPane = new JScrollPane(canvas);
		// canvas.setBorder(BorderFactory.createBevelBorder(5));
		// now for each terrainElement, add it to the TrayModel
		// by calling addTerrainElement()
		this.setLayout( new BorderLayout());
		this.add(trayPanel, BorderLayout.LINE_END);
		this.add(mapScrollPane, BorderLayout.CENTER);

		// now handle the mapgrid
		createMapGrid();
	}

	private void createMapGrid() {
		numCol = manager.getMapGridCol();
		numRow = manager.getMapGridRow();
		
		mapGrid = new ImageElement[numCol][numRow];	
		setMapSize();	
	}

	private void setMapSize() {
		int w = numCol * mapTileWidth;
		int h = numRow * mapTileHeight;
		mapSize = new Dimension(w,h);
	}
	
	public void mapTileAdded( ImageElement el ) {
		// assume that all tiles have the same size
		ImageIcon thumbnailIcon = el.getSmallIcon();
		mapTileWidth = thumbnailIcon.getIconWidth();
		mapTileHeight = thumbnailIcon.getIconHeight();
		trayMod.addElement(el);
		trayPanel.setModel(trayMod);
	}
	
	public void mapTileDeleted( ImageElement el ) {
		Debug.out("mapTileDeleted TBC");
	}
	
	// need to get the new map size
	public void mapSizeChanged() {
		ImageElement[][] oldMapGrid = mapGrid;
		int oldNumCol = numCol;
		int oldNumRow = numRow;
		createMapGrid();
		
		// now copy the old stuff into the new grid
		for (int i=0; i< numCol; i++) {
			for (int j=0; j < numRow; j++) {
				// check if this exist in the old world
				if ( (i< oldNumCol) && (j < oldNumRow)) {
					// yes this one is in the old one too, move it over
					mapGrid[i][j] = oldMapGrid[i][j];
				} else {
					// this one is NOT in the old, but is in new
				}
			}
		}		
		canvas.repaint();
	}
	
	public MapModel store() {
		MapModel mod = new MapModel();
		mod.setMapImageArray(mapGrid);
		mod.setNumOfCol(numCol);
		mod.setNumOfRow(numRow);
		return ( mod );
	}
	
	public void load(MapModel mod) {
		mapGrid = mod.getMapImageArray();
		numCol = mod.getNumOfCol();
		numRow = mod.getNumOfRow();
		mapSizeChanged();			
		return;
	}
	
	/**
	 * This is the inner class that handles painting the map grid
	 * @author Milton  Soong
	 *
	 * To change the template for this generated type comment go to
	 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
	 */
	public class MapCanvas extends JPanel implements MouseListener, MouseMotionListener {
		
		int selectedGridX=0;
		int selectedGridY=0;
		MapBuilder parent;
		
		public MapCanvas(MapBuilder in) {
			parent = in;
			setDoubleBuffered(true);
			addMouseListener(this);
			addMouseMotionListener(this);
			// setBackground(Color.green);
		}
		
		protected  void paintComponent(Graphics g)  {
			super.paintComponent(g);
			// right now the assumption is that all terrainTiles are of the same size
			for ( int i=0, offsetX=0; i< numCol; i++, offsetX+= mapTileWidth ){
				for ( int j=0, offsetY=0; j< numRow; j++, offsetY+= mapTileHeight ){
					  // first paint the background map
					  //
					  ImageElement el = mapGrid[i][j]; 
					  if (  el != null ) {
					  	Image im = el.getSmallIcon().getImage();
						g.drawImage(im, offsetX, offsetY, null);
					  }
				}
			}
			// now draw a grid on top
			g.setColor(Color.RED);
			// first draw vertical lines
			for ( int i=0, offsetX=0; i<= numCol; i++, offsetX+= mapTileWidth ){
				g.drawLine(offsetX, 0, offsetX, (int)mapSize.getHeight()); 
			}
			// draw horizontal line
			for ( int j=0, offsetY=0; j<= numRow; j++, offsetY+= mapTileHeight ){
				g.drawLine(0, offsetY, (int)mapSize.getWidth(), offsetY); 
			}
		}
		
		public Dimension getPreferredSize() {
		  return ( mapSize );
		}

		public Dimension getMinimumSize() {
		  return ( mapSize );
		}

		public Dimension getMaximumSize() {
		  return ( mapSize );
		}
		
		public void mouseReleased(MouseEvent e){		
		}
		public void mousePressed(MouseEvent e){
			handleMouseInteraction(e);
		}
		
		public void mouseClicked(MouseEvent e){
		}
		
		public void mouseEntered(MouseEvent e){
		}
		public void mouseExited(MouseEvent e){
		}
		
		public void mouseMoved(MouseEvent e){
		}
		public void mouseDragged(MouseEvent e){
			handleMouseInteraction(e);
		}
		private void handleMouseInteraction(MouseEvent e){
			// figure out which grid the click fell on
			int x = e.getX();
			int y = e.getY();

			selectedGridX = x / mapTileWidth;
			selectedGridY = y / mapTileHeight;
			if ( (selectedGridX >= numCol) || (selectedGridY >= numRow)) {
				return;
			}
			
			ImageElement el = (ImageElement)Utils.lookup.get(Utils._TERRAIN_SELECTED_FROM_TRAY);
			if (el != null) {
				mapGrid[selectedGridX][selectedGridY]=el;
				//Debug.out("Set mapGrid=" +selectedGridX + "," +selectedGridY );
			} else {
				// it's null, do nothing
			}
			repaint();
			
		}
	}
}
